package crazy;

public class BubbleSort
{

	public static void main(String[] args)
	{
		DataWrap[] data = { new DataWrap(21, ""), new DataWrap(30, ""),
				new DataWrap(49, ""), new DataWrap(30, "*"),
				new DataWrap(16, ""), new DataWrap(9, "") };
		
		System.out.println("before sorting-->"+java.util.Arrays.toString(data));
		bubbleSort(data);
		System.out.println("after sorting-->"+java.util.Arrays.toString(data));
		
	}

	
	public static void bubbleSort(DataWrap[] data)
	{
		int arrayLength = data.length;
		for (int i = 0; i < arrayLength-1; i++)//做n-1趟扫描
		{
			boolean flag = false;
			for (int j = 0; j < arrayLength -1-i; j++)//(arrayLength -1-i) 次比较
			{
				if (data[j].compareTo(data[j+1])>0)
				{
					DataWrap tmp = data[j+1];
					data[j+1] = data[j];
					data[j] = tmp;
					flag = true;
				}
			}
			System.out.println(java.util.Arrays.toString(data));
			if (!flag)//如果没有发生交互，就结束循环
			{
				break;
			}
		}
	}
}
